#!/bin/bash
BakDir=/home/shuaihu/mysqlbak
BinDir=/var/lib/mysql
BinFile=/var/lib/mysql/binlog.index
gzfile(){
    for file in `ls $1`
    do
        if [ ${file##*.}x = "gz"x ]
        then
            echo $1/$file
            return 1
        fi
    done
    echo ""
    return 0
}
if [ $1 ]
then
    week=`date -d $1 +%w`
    YMD=`date -d "-$week day $1" +%Y%m%d`
    FullBakDir=$BakDir/$YMD
    if [ -d $FullBakDir ]
    then
        fullbakfile=$(gzfile $FullBakDir)
        if [ -n "$fullbakfile" ]
        then
            echo "开始全量恢复..."
            gunzip <$fullbakfile|mysql -uroot >/dev/null 2>&1
            echo  "全量恢复完成。"
            IncFile=$FullBakDir/$1
            if [ -f $IncFile ]
            then
                echo "开始增量恢复..."
                for file in `cat $IncFile`
                do
                    mysqlbinlog $file |mysql -uroot >/dev/null 2>&1
                done
                echo "增量恢复完成。"
            fi
        else
            echo 指定日期的备份数据不存在！
        fi
    else
    echo 指定日期的备份数据不存在！
    fi
else
    echo -e "use:\n restore 20210228"
fi
